package com.showcai.controller.product;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.showcai.controller.base.BaseController;
import com.showcai.def.Constant;
import com.showcai.module.Certificate;
import com.showcai.module.CertificateUserdLog;
import com.showcai.module.Product;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2017-7-3.
 */
@RequestMapping("/product")
@Controller
public class ProductContorller extends BaseController {

    @Autowired
    ProductService productService;
    @Autowired
    ProductEntityService productEntityService;

    @RequestMapping(value = "/getProductList")
    public ModelAndView productList(Integer page, Integer pageSize) {
        System.out.println("进入到了这个控制器中——————");
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/product/productList");
        Map<String, String> map = this.showRequestParameters();
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        PageInfo<Product> pageinfo = productService.getProductList(page,pageSize);
        mov.addObject("pageinfo", pageinfo);
        mov.addObject("bean", map);
        return mov;
    }


    @RequestMapping(value = "/product")
    public ModelAndView certificate(Long tid) {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/product/product");
        if(tid!=null){
            Product product=productService.getProductByTid(tid);
            mov.addObject("product",product);
        }
        return mov;
    }

    /**
     * 修改实体方法
     *
     * @param product
     * @return
     */
    @RequestMapping(value = "/modifyProduct")
    @ResponseBody
    public String modifyProduct(Product product) {
        ResultObj resultObj=new ResultObj();
        try {
            productService.modifyProduct(product);
            resultObj.setCode(ReturnCode.SUCCESS.getCode());
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }
    @RequestMapping(value = "/stopProduct")
    @ResponseBody
    public String stopProduct(Long productId) {
        ResultObj resultObj=new ResultObj();
        productService.stopProduct(productId);
        productEntityService.stopAllProductEntity(productId);
        resultObj.setCode(ReturnCode.SUCCESS.getCode());
        resultObj.setMsg(ReturnCode.SUCCESS.getMsg());
        return resultObj.toJson();
    }
    @RequestMapping(value = "/operateProduct")
    @ResponseBody
    public String operateProduct(Long productId) {
        ResultObj resultObj=new ResultObj();
        productService.operateProduct(productId);
        resultObj.setCode(ReturnCode.SUCCESS.getCode());
        resultObj.setMsg(ReturnCode.SUCCESS.getMsg());
        return resultObj.toJson();
    }

}
